package com.sinobpo.rnd2.ehr.web.interceptor;

import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.sinobpo.rnd2.ehr.model.User;

public class LoginInterceptor extends MethodFilterInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		ActionContext cxt = invocation.getInvocationContext();
		if ((User) cxt.getSession().get("user") != null) {
			invocation.invoke();
		} else {
			// HttpServletRequest request=ServletActionContext.getRequest();
			/*
			 * HttpServletRequest reque ServletActionContext.getRequest();
			 * String path=request.getRequestURI(); String
			 * actionPath=".."+path.substring(9); //访问服务器所带有的参数信息 String
			 * queryInfo=request.getQueryString();
			 * if(queryInfo!=null&&(!queryInfo.equals(""))){
			 * actionPath=actionPath+"?"+queryInfo; }
			 */

		/*	HttpServletRequest request = (HttpServletRequest) cxt
					.get(StrutsStatics.HTTP_REQUEST);
			String path = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+request.getRequestURI();
			//访问地址不是注销时才设置登录前的url在Session中
			if(!request.getRequestURI().equals("user_loginOut") &&
					!request.getRequestURI().equals("user_loginOut.action"))
			cxt.getSession().put("uri",path);*/
			HttpServletRequest request=ServletActionContext.getRequest();
			HttpServletResponse response=ServletActionContext.getResponse();
			PrintWriter pw = response.getWriter();
			if (request.getHeader("X-Requested-With") != null
					&& request.getHeader("X-Requested-With").equalsIgnoreCase(     
					"XMLHttpRequest")) {  
				
				   response.sendError(408,"您未登录!");
				   return null;
			}else
				return Action.LOGIN;
		}
		return null;
	}

}
